package common;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import org.apache.commons.lang.WordUtils;

public class JSONArray {
	
	public static String formatJSONToSelect(List list, String valorValue, String valorText) {
		
		 
		StringBuffer respuesta = new StringBuffer();
		String a = null ;
		try {
			
		respuesta.append("[");
			
		String value = "get" + valorValue;
		String text = "get" + valorText;
		
		for (Object object : list) {
			
			Method[] methods = object.getClass().getMethods();
			
			for (int i = 0; i < methods.length; i++) {
				if ( methods[i].getName().equalsIgnoreCase(value))
					value =methods[i].getName();
				if ( methods[i].getName().equalsIgnoreCase(text))
					text =methods[i].getName();
			}
			
			Method methodValue = object.getClass().getMethod(value);
			String resultValue= methodValue.invoke(object).toString();
			
			Method methodText = object.getClass().getMethod(text);
			String resultText = methodText.invoke(object).toString();
			
			respuesta.append( "{\"Value\":\"" + resultValue +"\"," + "\"Text\":\"" + resultText + "\" }," );		
			
		}
		a = respuesta.toString();
		a = a.substring(0, a.length()-1);
		
		//respuesta.append("]");
		
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return a + "]";
	
	}
	
	public static String formatJSONToSelect(List list, String valorValue, String valorText, String valorText2) {
		
		
		StringBuffer respuesta = new StringBuffer();
		String a = null ;
		try {
			
		respuesta.append("[");
			
		String value = "get" + valorValue;
		String text = "get" + valorText;
		String text2 = "get" + valorText2;
		
		for (Object object : list) {
			
			Method[] methods = object.getClass().getMethods();
			
			for (int i = 0; i < methods.length; i++) {
				if ( methods[i].getName().equalsIgnoreCase(value))
					value =methods[i].getName();
				if ( methods[i].getName().equalsIgnoreCase(text))
					text =methods[i].getName();
				if ( methods[i].getName().equalsIgnoreCase(text2))
					text2 =methods[i].getName();
			}
			
			Method methodValue = object.getClass().getMethod(value);
			String resultValue= methodValue.invoke(object).toString();
			
			Method methodText = object.getClass().getMethod(text);
			String resultText = methodText.invoke(object).toString();
			
			Method methodText2 = object.getClass().getMethod(text2);
			String resultText2 = methodText2.invoke(object).toString();
			
			respuesta.append( "{\"Value\":\"" + resultValue +"\"," + "\"Text\":\"" + resultText +", "+ resultText2 + "\" }," );		
			
		}
		a = respuesta.toString();
		a = a.substring(0, a.length()-1);
		
		//respuesta.append("]");
		
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return a + "]";
	
	}
	
	public static String formatBeanToJSON(Object o){
		
		StringBuffer buffer = new StringBuffer();
		String respuesta = "";
		
		try{
			
		buffer.append("[");
		
		Method[] methods = o.getClass().getMethods();
		for (int i = 0; i < methods.length; i++) {
			if ( methods[i].getName().contains("get")){
			String m = methods[i].getName();
			Method methodValue = o.getClass().getMethod(m);
			if( methodValue.invoke(o) != null){
				String resultValue= methodValue.invoke(o).toString();
				if( methodValue.getName().contains("get") && 
						!methodValue.getReturnType().equals(java.util.Set.class) && 
						!methodValue.getReturnType().equals(List.class) &&
						!methodValue.getReturnType().equals(java.util.ArrayList.class) &&
						!methodValue.getReturnType().equals(Class.class)){
					
					String methodString =  methodValue.getName().substring(3, methodValue.getName().length() );
					String uncapitalize = WordUtils.uncapitalize(methodString);
					buffer.append( "{\"id\":\"" + uncapitalize +"\"," + "\"value\":\"" + resultValue +"\" }," );		
				}
			}
			}
		}
		respuesta = buffer.toString();
		respuesta = respuesta.substring(0, respuesta.length()-1) + "]";
		
		
	} catch (SecurityException e) {
		e.printStackTrace();
	} catch (NoSuchMethodException e) {
		e.printStackTrace();
	} catch (IllegalArgumentException e) {
		e.printStackTrace();
	} catch (IllegalAccessException e) {
		e.printStackTrace();
	} catch (InvocationTargetException e) {
		e.printStackTrace();
	}
		
		
	return respuesta;
	
	}

}
